package by.grsu.shakh.page.librarian;

import java.util.Date;
import java.util.List;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.ListChoice;
import org.apache.wicket.markup.html.form.SubmitLink;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.CompoundPropertyModel;

import by.grsu.shakh.app.LibraryWebApplication;
import by.grsu.shakh.dao.BookDao;
import by.grsu.shakh.dao.CatalogDao;
import by.grsu.shakh.entity.Book;

/**
 * "edit" page for Book object.
 * 
 * @author shakh_andrew
 */
public class BookEditPage extends WebPage {

	public BookEditPage(final Book book) {
		super();

		final Form<Book> form = new Form<Book>("form",
				new CompoundPropertyModel<Book>(book));
		add(form);

		form.add(new TextField<String>("name"));
		final CatalogDao catalogDao = new CatalogDao(
				LibraryWebApplication.DATA_XML_FOLDER);
		final List<Long> catalog = catalogDao.getAllID();
		final ListChoice listChoice = new ListChoice("catalogid", catalog);
		form.add(listChoice);
		form.add(new TextField<String>("author"));
		form.add(new TextField<Date>("date"));
		form.add(new TextField<String>("publishing"));
		form.add(new TextField<String>("description"));

		form.add(new SubmitLink("save") {
			@Override
			public void onSubmit() {
				super.onSubmit();
				final BookDao bookDao = new BookDao(
						LibraryWebApplication.DATA_XML_FOLDER);
				if (book.getId() == null) {
					bookDao.saveNew(book);
				} else {
					bookDao.update(book);
				}
				setResponsePage(new ListBookPage());
			}
		});

		form.add(new Link("cancel") {
			@Override
			public void onClick() {
				setResponsePage(new ListBookPage());

			}
		});
	}
}
